// objects.c++
// objects class implementation file

#include "objects.h"

Objects::Objects() : objects_() {
	nFountains_ = 0;
	nWizards_ = 0;
}

Objects::~Objects() {
	while (!objects_.empty()) {
		delete objects_.back();
		objects_.pop_back();
	}
}

unsigned int Objects::getNObjects() {
	return objects_.size();
}

void Objects::removeObject(const unsigned int objectNo) {
	if (objects_.at(objectNo)->getObjectType() == FOUNTAIN) {
		--nFountains_;
	} else if (objects_.at(objectNo)->getObjectType() == WIZARD) {
		--nWizards_;
	} else {
		// error
	}
	delete objects_.at(objectNo);
	objects_.erase(objects_.begin() + objectNo);
}

Object* Objects::getObject(const unsigned int objectNo) {
	return objects_.at(objectNo);
}

unsigned int Objects::getNFountains() {
	return nFountains_;
}

void Objects::addFountain(const unsigned int x, const unsigned int y) {
	Fountain* fountain = new Fountain(x, y);
	objects_.push_back(fountain);
	++nFountains_;
}

void Objects::removeFountain(const unsigned int fountainNo) {
	unsigned int nFountains = 0;
	for (unsigned int objectNo = 0; objectNo < objects_.size(); ++objectNo) {
		if (objects_.at(objectNo)->getObjectType() == FOUNTAIN) {
			if (nFountains == fountainNo) {
				delete objects_.at(objectNo);
				objects_.erase(objects_.begin() + objectNo);
				--nFountains_;
				return;
			}
			++nFountains;
		}
	}
	// error
}

Fountain* Objects::getFountain(const unsigned int fountainNo) {
	unsigned int nFountains = 0;
	for (unsigned int objectNo = 0; objectNo < objects_.size(); ++objectNo) {
		if (objects_.at(objectNo)->getObjectType() == FOUNTAIN) {
			if (nFountains == fountainNo) {
				return (Fountain*)objects_.at(objectNo);
			}
			++nFountains;
		}
	}
	return NULL; // error
}

unsigned int Objects::getNWizards() {
	return nWizards_;
}

void Objects::addWizard(const unsigned int x, const unsigned int y,
                        const unsigned int clientId) {
	Wizard* wizard = new Wizard(x, y, clientId);
	objects_.push_back(wizard);
	++nWizards_;
}

void Objects::removeWizard(const unsigned int wizardNo) {
	unsigned int nWizards = 0;
	for (unsigned int objectNo = 0; objectNo < objects_.size(); ++objectNo) {
		if (objects_.at(objectNo)->getObjectType() == WIZARD) {
			if (nWizards == wizardNo) {
				delete objects_.at(objectNo);
				objects_.erase(objects_.begin() + objectNo);
				--nWizards_;
				return;
			}
			++nWizards;
		}
	}
	// error
}

Wizard* Objects::getWizard(const unsigned int wizardNo) {
	unsigned int nWizards = 0;
	for (unsigned int objectNo = 0; objectNo < objects_.size(); ++objectNo) {
		if (objects_.at(objectNo)->getObjectType() == WIZARD) {
			if (nWizards == wizardNo) {
				return (Wizard*)objects_.at(objectNo);
			}
			++nWizards;
		}
	}
	return NULL; // error
}

